package com.itheima.controller;

import com.itheima.domain.Teacher;
import com.itheima.service.TeacherService;

import java.util.ArrayList;
import java.util.Scanner;

/*
老师用户交互类
增
删
改
查

键盘录入和 输出语句
调用 service
 */
public class OtherTeacherController {
        private Scanner sc = new Scanner(System.in);
    private TeacherService teacherService= new TeacherService();


    //定义方法 作为 老师系统的入口
    public  void start(){
        Scanner sc = new Scanner(System.in);
        //死循环
        loop:
        while (true) {

            //键盘录入
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");    //选择
            String choice = sc.next();
            switch (choice) {
                case "1":
                   // System.out.println("新增");
                    addTeacher();
                    break;
                case "2":
                    deleteTeacher();
                    //System.out.println("删除");
                    break;
                case "3":
                    updateTeacher();
                    //System.out.println("修改");
                    break;
                case "4":
                    findAllTeachers();
                    //System.out.println("查看");
                    break;
                case "5":
                    System.out.println("再见");
                    //break loop;
                    return;
                default:
                    System.out.println("输入有误");
                    break;
            }
        }
    }

    //增
    public void addTeacher(){
        System.out.println("请输入工号");
        String sid = null;
        while (true) {
            sid = sc.next();
            boolean exist = teacherService.isExist(sid);
            if(exist){
                System.out.println("已经存在,请重新录入");
            }else {
               break;
            }
        }
        Teacher teacher = luruXinxi(sid);
        teacherService.addTeacher(teacher);
        System.out.println("添加成功");


    }

    public Teacher luruXinxi(String sid) {
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        int age = sc.nextInt();
        System.out.println("请输入生日");
        String birthday = sc.next();
        Teacher student = new Teacher(sid,name,age,birthday);
        return student;
    }

    //删
    public void deleteTeacher(){
        ArrayList<Teacher> list = teacherService.findAllTeachers();
        if (list==null||list.size()==0){
            System.out.println("没有数据请先添加");
            return;
        }
        String sid = luruGongHao();
        teacherService.deleteTeacher(sid);
        System.out.println("删除成功");

    }

    public String luruGongHao() {

        System.out.println("请输入工号");
        String sid ;
        while (true) {
            sid = sc.next();
            boolean exist = teacherService.isExist(sid);
            if(!exist){
                System.out.println("不存在,请重新录入");
            }else {
                break;
            }
        }
        return sid;
    }

    //改
    public void updateTeacher(){
        ArrayList<Teacher> list = teacherService.findAllTeachers();
        if (list==null||list.size()==0){
            System.out.println("没有数据请先添加");
            return;
        }
        String sid = luruGongHao();
        Teacher teacher = luruXinxi(sid);
        teacherService.updateTeacher(sid,teacher);
        System.out.println("修改成功");

    }

    //查
    public void findAllTeachers(){
        ArrayList<Teacher> list = teacherService.findAllTeachers();
        if (list==null||list.size()==0){
            System.out.println("没有数据请先添加");
            return;
        }
        System.out.println("工号\t姓名\t年龄\t生日");
        for (int i = 0; i < list.size(); i++) {
            Teacher teacher = list.get(i);
            System.out.println(teacher.getSid() + "\t" + teacher.getName() + "\t" + teacher.getAge() + "岁\t" + teacher.getBirthday());
        }

    }

}
